package fede.audiomod.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class AudiomodFunctions {
	
	public static boolean writeFilePiece(File input, File output, long start, long offset) {
		// Condizioni di uscita
		if(!input.exists())			return false;
		if(!input.isFile())			return false;
		if(output.exists())			return false;
		if(start<0 || start>input.length())				return false;
		if(offset<1 || (start+offset)>input.length())	return false;
		
		
		try {
			output.createNewFile();
			InputStream ins = new FileInputStream(input);
			OutputStream outs = new FileOutputStream(output);
			byte[] arr = new byte[(int) offset];
			
			ins.skip(start);
			ins.read(arr);
			outs.write(arr);
			
			ins.close();
			outs.close();
			
			return true;
		} catch (IOException e) {
			return false;
		}
	}
	
	public static boolean writeFilePiece(File output, byte[] barr) {
		// Condizioni di uscita
		if(output.exists())			return false;
		if(barr.length < 1)			return false;
		
		
		try {
			output.createNewFile();
			OutputStream outs = new FileOutputStream(output);
			outs.write(barr);
			outs.close();
			return true;
		} catch (IOException e) {
			return false;
		}
	}
	
	
}